$(function(){
	/**项目指标树*/
	$('#wftype_tree').tree({
	    url : 'wfType/wfTypes.shtml',
	    onLoadSuccess:function(node,data){
	         var t = $(this);
	  		 if(data){
			     $(data).each(function(index,d){
					if(this.state == 'closed'){
					    t.tree('expandAll');
					}
			     });
			}
	    },
	    onClick : function (node) {
	    	//console.log(node);
	    	$('#dg').datagrid('reload', {
	    		wftypeId : node.nodeKey
	    	});
	    }
	});
	
	/** 新增 */
	$('#add').click(function(){
		var _node = $('#wftype_tree').tree('getSelected');
		if (null == _node) {
			$.messager.alert('warning', '请选择流类型！', 'warning');
		} else {
			var _url = 'workflow/designer.shtml?wftypeId=' + _node.nodeKey;
			showEasyUiWindow('contentFrame', _url, 'win', '新增流程定义', 500, 375, true);
		}
	});
	
	/*** 编辑 */
	$('#edit').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			var url = 'workflow/designer.shtml?workFlowId=' + row.workFlowId + '&wftypeId=' + row.wftypeId;
			showEasyUiWindow('contentFrame', url, 'win', '编辑流程定义', 500, 375, true);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 删除 */
	$('#remove').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要删除此条数据吗?', function(r) {
				if (r) {
					var workFlowId = row.workFlowId;				
					$.get('workflow/delete.shtml', {workFlowId : workFlowId}, function(rs) {
						if (rs.success == true) {
							reSearch();
							$.messager.alert('warning', rs.message, 'warning');
						} else {
							$.messager.alert('warning', rs.message, 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/**启动*/
	$('#start').click(function(){
		var row = $('#dg').treegrid('getSelected');
		if (row) {
			var url = 'workflow/testStart.shtml?workFlowId=' + row.workFlowId + '&wftypeId=' + row.wftypeId;
			$.post(url,{workFlowId : row.workFlowId, wftypeId : row.wftypeId}, function(data){
				$.messager.alert('warning', data.success + ',' + data.message, 'warning');
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
});

/**
 * 重载
 */
function reSearch() {
	doSearch();
}


function doSearch() {
	$('#dg').datagrid('reload');
}
